package com.example.jikeyoujikeyou.office_sz.view.activity.register;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseFragment;

public class VPhoneFragment extends BaseFragment {

    private Button btn_register_getVerifyCode,btn_register_next;
    private TextView tv_getVerifyCodeMessage;
    private EditText et_telephone,et_verifyCode;
    private View activity_register_telephone;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity_register_telephone = inflater.inflate(R.layout.activity_register_telephone,null,false);
        init();
        return activity_register_telephone;
    }

    private void init() {
        btn_register_getVerifyCode = (Button) activity_register_telephone.findViewById(R.id.btn_register_getVerifyCode);
        btn_register_next = (Button) activity_register_telephone.findViewById(R.id.btn_register_next);
        tv_getVerifyCodeMessage = (TextView) activity_register_telephone.findViewById(R.id.tv_getVerifyCodeMessage);
        et_telephone = activity_register_telephone.findViewById(R.id.et_telephone);
        et_verifyCode = activity_register_telephone.findViewById(R.id.et_verifyCode);

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void afterTextChanged(Editable editable) {
                String et_telephone_value = et_telephone.getText().toString();
                String et_verifyCode_value = et_verifyCode.getText().toString();
                if (et_telephone_value != null && et_verifyCode_value != null && et_telephone_value.length() == 11 && et_verifyCode_value.length() == 4 && et_verifyCode_value.equals("1234")) {
                    Handler enableButton = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            btn_register_next.setEnabled(true);
                        }
                    };
                    enableButton.sendMessage(enableButton.obtainMessage());
                } else {
                    Handler enableButton = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            btn_register_next.setEnabled(false);
                        }
                    };
                    enableButton.sendMessage(enableButton.obtainMessage());
                }
            }
        };

        et_telephone.addTextChangedListener(textWatcher);
        et_verifyCode.addTextChangedListener(textWatcher);

        btn_register_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.staff.setPhone(et_telephone.getText().toString());
                RegisterActivity.staff.setCode(et_verifyCode.getText().toString());
                message(null);
            }
        });

        btn_register_getVerifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String telephone = et_telephone.getText().toString();
                if (null == telephone || telephone.trim().equals("") || telephone.length() != 11) {
                    new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("手机号码错误").setPositiveButton("确定", null).create().show();
                    return ;
                } else {
                    new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("验证码：1234").setPositiveButton("确定", null).create().show();
                }

                final Handler showTextAndDisableButton = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        tv_getVerifyCodeMessage.setVisibility(TextView.VISIBLE);
                        btn_register_getVerifyCode.setEnabled(false);
                    }
                };
                final Handler handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        btn_register_getVerifyCode.setText(msg.arg1 + "s");
                    }
                };
                final Handler hiddenShowTextAndEnableButton = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        btn_register_getVerifyCode.setText("点击获取");
                        tv_getVerifyCodeMessage.setVisibility(TextView.INVISIBLE);
                        btn_register_getVerifyCode.setEnabled(true);
                    }
                };
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        showTextAndDisableButton.sendMessage(showTextAndDisableButton.obtainMessage());
                        for (int i = 60;i > 0;i--) {
                            Message message = handler.obtainMessage();
                            message.arg1 = i;
                            handler.sendMessage(message);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        hiddenShowTextAndEnableButton.sendMessage(hiddenShowTextAndEnableButton.obtainMessage());
                    }
                }).start();
            }
        });

    }
}
